let uglify=require('uglify-js');
let httpProxy = require("http-proxy");
let fs=require('fs');
let PORT=80;       
let mode='';      //dev || build
// codeServer #######################################################################################################################################
let options = { 
     compress: true,     // 启用压缩 
     mangle: true,       // 启用混淆 
     output: { 
         comments: false // 移除注释 
       } 
   };

/**
 * Processing of JavaScript and CSS style codes, including (translation and compression)
 * If you are using some of the latest features of ECMAScript, you may also need "Babel" for additional transpilation.
 * 
 * @param {*} data ={
 *                     path:'',           // filePath  
 *                     script:{
 *                         code:'....',   // code text
 *                          
 *                     },
 *                     css:{
 *                         type:'',       // css || less || sass ....
 *                         code:'....'    // css  text  
 *                     } 
 *                  }
 * @param {*} callBack  (translatedCode ,  translatedCssCode )
 */
let codeServer=function(data,callBack){
      //
      let script=data.script;
      let scriptCode=script.code;
      //
      let css=data.css;
      let cssType=css.type,cssCode=css.code;

          /* 
             1. Do something , For example:  translate or compress  code .
             2. You can also extract url information from css here and replace it with base64. 

             But here I only provide ideas, because everyone's project is different, 
             and what I do is not the best or the simplest, but may make the project 
             more complicated. So everything is left to you.
          
          */

          if(mode==='build'){
               scriptCode=uglify.minify(scriptCode,options);
          }else{

          }; 
          callBack(scriptCode , cssCode );
};
// ajaxServer ##########################################################################################################################################
/**
 * Process ajax requests 
 * @param {*} res 
 * @param {*} query 
 * @param {*} data
 * @returns {boolean}   true: ajax request , false: normal   
 */
let ajaxServer=function(res , query , data ){
        /* 
          if(....){
               res.writeHead(200,{'Content-Type':'json'});
               res.write('{msg:success}'); 
               res.end();
               // or
               res.write('404'); 
               res.end();

               return true;
          };
        */ 
       return false;
};


// proxyServer ########################################################################################################################################
let proxyServer=null
{
     let proxy = httpProxy.createProxyServer({
               target: 'http://localhost:8081/',    // interface address
               // The settings below are for https
               // ssl: {
               //     key: fs.readFileSync('server_decrypt.key', 'utf8'),
               //     cert: fs.readFileSync('server.crt', 'utf8')
               // },
               // secure: false
          });

          proxy.on('error', function (err, req, res) {
               res.writeHead(500, {
                    'content-type': 'text/plain'
               });
               console.log(err);
               res.end('Something went wrong. From proxyServer');
          });

     /**
      * Judge and intercept proxy requests
      * @param {*} req 
      * @param {*} res 
      * @param {*} url  
      * @param {*} query
      * @returns {boolean}   true: proxy request , false: normal   
      */
     proxyServer=function(req , res , url , query){
          /* 
          // Make judgments about requests that require proxying
          if(url.indexOf('xxx.com')>-1){
               proxy.web(req, res);

               return true;
          }; 
          */
          return false;
     };
};




//#####################################################################################################################
module.exports={
     codeServer:codeServer,
     proxyServer:proxyServer,
     ajaxServer:ajaxServer,
     port:PORT,
     init:(type)=>{
           mode=type||'dev';
     }
}

